/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controllers;

import com.entities.Company;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

    
/**
 *
 * @author ben
 */
@Stateless
public class CompanyController {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @PersistenceContext
    EntityManager em;
    
    private static final Logger logger = Logger.getLogger(CompanyController.class.getName());
    private Company company;
    
    
    public Long save(Company company)
    {
        try {
            em.persist(company);
            System.out.println("Saving Company complete");
            logger.info("Saving company complete");
            return company.getId();
        } catch (Exception e) {
            System.out.println("Error saving Company"  + e.toString());
            logger.log(Level.SEVERE, "Error saving Company");
            return -1L;
        }
    }
    
    public List<Company> getAllCompanies()
    {
        try {
             Query findCompanyQuery = em.createNamedQuery("Company.findAll");
            List<Company> companies= findCompanyQuery.getResultList();
            System.out.println("Found list of companies");
            return companies;
            
        } catch (Exception e) {
            e.printStackTrace();
            return null;
            
        }
    }
    
    public Company findCompany(Long id)
    {
        try {
            company = em.find(Company.class, id);
            logger.log(Level.INFO, "Company found : {0}", company.getId().toString());
            return company;
            
        } catch (Exception e) {
                        logger.log(Level.SEVERE, "Could not find Company");
                        return null;

        }
           
    
    }
}
